package com.fuchuang.seckillsystem.controller;

import com.fuchuang.seckillsystem.entity.Role;
import com.fuchuang.seckillsystem.service.IRoleService;
import com.fuchuang.seckillsystem.vo.GrantVo;
import com.fuchuang.seckillsystem.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    
    @Autowired
    private IRoleService roleService;
    
    @PostMapping("grant")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public RespBean grant(@RequestBody GrantVo grantVo) {
        roleService.grant(grantVo.getUserId(), grantVo.getRoleIds());
        return RespBean.success();
    }
    
    @GetMapping("userRole")
    public RespBean getUserRole(@RequestParam("userId") Integer userId) {
        List<Role> roles = roleService.getUserRole(userId);
        return RespBean.success(roles);
    }
    
    @GetMapping("allRole")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_FORM')")
    public RespBean allRole() {
        List<Role> roles = roleService.getAllRole();
        return RespBean.success(roles);
    }
}
